<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="com.example.studentgrades.model.Admin" %>
<%@ page import="com.example.studentgrades.model.Student" %>
<%@ page import="com.example.studentgrades.dao.StudentDAO" %>
<%
    Admin admin = (Admin) session.getAttribute("admin");
    if (admin == null) {
        response.sendRedirect("admin_login.jsp");
        return;
    }

    int studentId = Integer.parseInt(request.getParameter("id"));
    Student student = null;

    try (Connection connection = com.example.studentgrades.util.ConnectionManager.getConnection()) {
        StudentDAO studentDAO = new StudentDAO(connection);
        student = studentDAO.findStudentById(studentId);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (student == null) {
        response.sendRedirect("manage_students.jsp");
        return;
    }
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑学生</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h2 class="text-center">编辑学生</h2>
    <form action="<%= request.getContextPath() %>/admin/update_student" method="post" class="mt-4">
        <input type="hidden" name="id" value="<%= student.getStudentId() %>">
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" class="form-control" id="username" name="username" value="<%= student.getUsername() %>" required>
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" class="form-control" id="password" name="password" value="<%= student.getPassword() %>" required>
        </div>
        <button type="submit" class="btn btn-primary btn-block">更新</button>
    </form>
    <a href="manage_students.jsp" class="btn btn-secondary btn-block mt-3">返回</a>
</div>
</body>
</html>
